package com.yanqu.road.dataHelper.repair.logic;

import com.yanqu.road.dataHelper.gameServer.GameServer;
import com.yanqu.road.dataHelper.manager.DataMgr;
import com.yanqu.road.dataHelper.repair.BaseRepair;
import com.yanqu.road.entity.activity.UserActivityConditionData;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author wpj
 * @Date 2020/8/19
 */
public class GetRankExcel extends BaseRepair {

    public GetRankExcel(GameServer gameServer) {
        super(gameServer);
    }

    @Override
    public void repair(long param1, String param2) {
        List<Map<String, String>> listMap = new ArrayList<>();
        List<Integer> activityIdList =  StringUtils.stringToIntegerList("9000003,9000004,9000005,9000006,9000007,9000009,9000010,9000080,9000081,9000082,9000083,9000084,9000086,9000087,9000153,9000154,9000155,9000157,9000158,9000204,9000205,9000206,9000207,9000208,9000210,9000211,9000280,9000281,9000307,9000308,9000334,9000335,9000358,9000359,9000373,9000391,9000392,9000407,9000408,9000423,9000424,9000438,9000456,9000457,9000472,9000473,9000488,9000489,9000512,9000513,9000527,9000545,9000546,9000561,9000562,9000585,9000586,9000609,9000610,9000624,9000642,9000643,9000658,9000659,9000676,9000677,9000700,9000701,9000715,9000733,9000734,9000749,9000750,9000765,9000766,9000789,9000790,9000804,9000822,9000823,9000838,9000839,9000856,9000857,9000880,9000881,9000895,9000913,9000914,9000929,9000930,9000949,9000950,9000973,9000974,9000988,9001006,9001007,9001022,9001023,9001038,9001075,9001076,9001099,9001100", ",");
        for (int activityId : activityIdList) {
            List<UserActivityConditionData> list = getRank(activityId);
            int i = 0;
            for (UserActivityConditionData userActivityConditionData : list) {
                i++;
                Map<String, String> map = new HashMap<>();
                map.put("activityId", activityId + "");
                map.put("userId", userActivityConditionData.getUserId() + "");
                map.put("type", userActivityConditionData.getType() + "");
                map.put("serverId", getGameServer().getServerInfo().getServerId() + "");
                map.put("rank", i + "");
                map.put("preValue", userActivityConditionData.getPreValue().toString());
                map.put("value", userActivityConditionData.getValue().toString());
                listMap.add(map);
            }
        }
        DataMgr.addData(listMap);
    }

    private List<UserActivityConditionData> getRank(int activityId) {
        List<UserActivityConditionData> dataList = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            call = getGameServer().getConnection().prepareCall("select * from t_u_activity_condition where activityId=" + activityId +" group by userId order by value+0 desc limit 400");
            rs = call.executeQuery();
            while (rs.next()) {
                UserActivityConditionData data = parse(rs);
                dataList.add(data);
            }
        } catch (SQLException e) {
            getGameServer().getLogger().error("异常错误", e);
        } finally {
            getGameServer().closeConnection(call, rs);
            return dataList;
        }
    }

    public UserActivityConditionData parse(ResultSet rs) throws Exception{
        UserActivityConditionData data = new UserActivityConditionData();
        data.setUserId(rs.getLong("userId"));
        data.setActivityId(rs.getInt("activityId"));
        data.setConditionId(rs.getInt("conditionId"));
        String value = rs.getString("preValue");
        if(!StringUtils.isNullOrEmpty(value)) {
            data.setPreValue(new BigInteger(value));
        }
        value = rs.getString("value");
        if(!StringUtils.isNullOrEmpty(value)) {
            data.setValue(new BigInteger(value));
        }
        data.setGetReward(rs.getBoolean("isGetReward"));
        data.setModifyTime(rs.getLong("modifyTime"));
        data.setParam(rs.getString("param"));
        data.setType(rs.getInt("type"));
        return data;
    }

}
